package com.yuki.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yuki.api.annotation.LogType;
import com.yuki.api.annotation.SystemLog;
import com.yuki.api.annotation.UserLoginToken;
import com.yuki.api.common.CommonPage;
import com.yuki.api.common.CommonResult;
import com.yuki.api.model.OmsOrderReturnReason;
import com.yuki.api.service.IOmsOrderReasonService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: Yuki Tadayoshi
 * @time: 2021/3/26 12:26
 */
@RestController
@RequestMapping("/returnReason")
@CrossOrigin
public class OmsOrderReturnReasonController {
    @Reference(
            version = "1.0.0",
            interfaceName = "com.yuki.api.service.IOmsOrderReasonService",
            interfaceClass = IOmsOrderReasonService.class,
            timeout = 120000
    )
    private IOmsOrderReasonService service;

    @PostMapping(value = "/create")
    @UserLoginToken
    @ResponseBody
    @SystemLog(description = "添加退货原因",type = LogType.ORDER_ADD_RETURN_REASON)
    public CommonResult create(@RequestBody OmsOrderReturnReason returnReason) {
        CommonResult commonResult = null;
        try {
            service.create(returnReason);
            commonResult = CommonResult.success(1);
        } catch (Exception e) {
            commonResult = CommonResult.failed();
            e.printStackTrace();
        }
        return commonResult;
    }

    @GetMapping(value = "/list")
    @UserLoginToken
    @ResponseBody
    @SystemLog(description = "分页查询退货申请",type= LogType.ORDER_RETURN_APPLY)
    public CommonResult<CommonPage<OmsOrderReturnReason>> list(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        CommonPage commonPage = service.list(pageSize, pageNum);
        return CommonResult.success(commonPage);
    }

    @SystemLog(description = "删除退货原因",type= LogType.ORDER_DELETE_RETURN_REASON)
    @PostMapping(value = "/delete")
    @UserLoginToken
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<Long> id) {
        int count = service.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @PostMapping(value = "/update/status")
    @SystemLog(description = "修改退货原因启用状态",type= LogType.ORDER_UPDATE_RETURN_REASON_STATUS)
    @UserLoginToken
    @ResponseBody
    public CommonResult updateStatus(@RequestParam("status") Integer status, @RequestParam("ids") Long id) {
        CommonResult commonResult;
        try {
            service.updateStatus(id, status);
            commonResult = CommonResult.success(1);
        } catch (Exception e) {
            commonResult = CommonResult.failed();
        }
        return commonResult;
    }


    @PostMapping(value = "/update/{id}")
    @UserLoginToken
    @ResponseBody
    @SystemLog(description = "修改退货原因",type= LogType.ORDER_UPDATE_RETURN_REASON)
    public CommonResult update(@PathVariable Long id,@RequestBody OmsOrderReturnReason returnReason){
        CommonResult commonResult;
        try {
            service.update(id, returnReason);
            commonResult = CommonResult.success(1);
        }catch (Exception e){
            commonResult = CommonResult.failed();
            e.printStackTrace();
        }
        return commonResult;
    }

    @GetMapping(value = "/{id}")
    @UserLoginToken
    @ResponseBody
    @SystemLog(description = "获取退货原因",type= LogType.ORDER_RETURN_REASON)
    public CommonResult<OmsOrderReturnReason> getItem(@PathVariable Long id){
        OmsOrderReturnReason omsOrderReturnReason = service.getOmsOrderReturnReason(id);
        return CommonResult.success(omsOrderReturnReason);
    }


}
